Include "data\gui\DevilGUI.bb"
Include "data\DevilShadowSystem.bb"
Include "data\ShadowVolumes.bb"
Include "data\UserInterface.bb"
Include "data\Filereq.bb"

AppTitle "Omega-Map"
Graphics3D 1024,768,32,2
SetBuffer BackBuffer()
SeedRnd MilliSecs()
HidePointer()
GUI_InitGUI("data\gui\Skins\BackRow.skin")

;!Locs
Local timer = CreateTimer(80)

;!Globs
Global fnt = LoadFont("Courier New", 20, True, False, False)
Global lbox1,lbox2
Global grp1,grp2
Global btnnew,btndel,btnapp,btnnew2
Global se
Global dummy = CreateCube()
Global lastse = dummy
Global rad1,rad22,rad3,mnu1,mnu2,mnu3,mnu4,mnufile
Global o.Objects,mo.Models
Global milli#
Global dummybrush = CreateBrush(255,255,255)
Global winkel#
Global shadmode = 0
HideEntity dummy
;!Gui
CreateToolBar()

;!SKY
;Himmel erstellen
sky=CreateMesh()
skyface=CreateSurface(sky,brush)
v0=AddVertex(skyface,0,160,0)
v1=AddVertex(skyface,-1000,-4,-1000)
v2=AddVertex(skyface,-1000,-4,+1000)
v3=AddVertex(skyface,+1000,-4,-1000)
v4=AddVertex(skyface,+1000,-4,+1000)
VertexColor skyface,v0,55,87,102
VertexColor skyface,v1,129,129,129
VertexColor skyface,v2,182,182,182
VertexColor skyface,v3,121,121,121
VertexColor skyface,v4,40,119,115
AddTriangle skyface,v0,v2,v1
AddTriangle skyface,v0,v4,v2
AddTriangle skyface,v0,v3,v4
AddTriangle skyface,v0,v1,v3
EntityFX sky,2 

;!Cam
Global cam = CreateCamera()
InitShadows(cam)

;!boden
tex = LoadTexture("textures\editgrid.png")
Global boden = CreatePlane()
EntityTexture boden,tex
PositionEntity boden,0,-2,0
SetShadowMesh(boden,False)

;!Light
Global light = CreateLight(2)
PositionEntity light,0,30,0
SetShadowLight(light)
;!Main_Loop
While Not KeyDown(1)
	WaitTimer timer
	
	se = SelectEntity()
	If se <> 0 Then
		EntityColor lastse,255,255,255
		lastse = se
		EntityColor se,255,0,0
	End If
	TransSelEntity()
	CameraMovement()
	
	If MouseX() < 1024-200 Then shadmode = 1-MouseDown(1)
	
	Render(shadmode)
	
	;Gui
	GUI_UpdateGUI()
	Update_ToolBar()
	Flip 0
	Cls
Wend

;!Functions

;Cam_Functions
Function CameraMovement()
	If MouseHit(3) Then
		MoveMouse GraphicsWidth()/2,GraphicsHeight()/2
	End If
	If MouseDown(3) Then
		mx# = Curve(MouseX()-GraphicsWidth()/2,mx,6)
		my# = Curve(MouseY()-GraphicsHeight()/2,my,6)
		RotateEntity cam,EntityPitch(cam)+my*3,EntityYaw(cam)-mx*3,0
		MoveMouse GraphicsWidth()/2,GraphicsHeight()/2
	End If
	
	move# = Curve((KeyDown(17)-KeyDown(31)),move,20)
	strafe# = Curve((KeyDown(32)-KeyDown(30)),strafe,20)
	MoveEntity cam,strafe*3,0,move*3
	
End Function




;GUI_FUNCTIONS
Function CreateToolBar()
	winmain = GUI_CreateWindow(1024-200, 0, 200, 768, "Toolbar","",False,False,False,False)
	;Menue
	mnufile = GUI_CreateMenu(winmain, "File")
	mnu1 = GUI_CreateMenu(mnufile, "New")
	mnu2 = GUI_CreateMenu(mnufile, "Open")
	mnu3 = GUI_CreateMenu(mnufile, "Save")
	GUI_CreateMenu(mnufile, "-")
	mnu4 = GUI_CreateMenu(mnufile, "Exit")
	
	;Modelliste
	grp1 = GUI_CreateGroupBox(winmain, 10, 20 , 180, 200 , "Models")
	lbox1 = GUI_CreateListBox(grp1, 10, 15, 160, 175)
	;.Items
	stream = ReadDir("models")
	Repeat
		
		file$ = NextFile(stream)
		If file$ <> "." And file$ <> ".." And (Right(file$,4) = ".3ds" Or Right(file$,4) = ".b3d")  Then
			GUI_Message(lbox1, "AddItem", -1, file$)
		End If
		
		If file$ = "" Then Exit
		
	Forever
	CloseDir(Stream)
	
	;Texture Liste
	grp2 = GUI_CreateGroupBox(winmain, 10, 230 , 180, 200 , "Textures")
	lbox2 = GUI_CreateListBox(grp2, 10, 15, 160, 175)
	GUI_Message(lbox2, "AddItem", -1, "NONE")
	;.Items
	stream = ReadDir("textures")
	Repeat
		file$ = NextFile(stream)
		If file$ <> "." And file$ <> ".." And (Right(file$,4) = ".bmp" Or Right(file$,4) = ".jpg" Or Right(file$,4) = ".png") Then
			GUI_Message(lbox2, "AddItem", -1, file$)
		End If
		
		If file$ = "" Then Exit
		
	Forever
	CloseDir(Stream)
	
	;Controls
	grp4 = GUI_CreateGroupBox(winmain, 10, 460 , 180, 180 , "Controls")
	btnnew = GUI_CreateButton(grp4, 15, 25, 150, 25, "New Model")
	btnnew2 = GUI_CreateButton(grp4, 15, 60, 150, 25, "New Object")
	btndel = GUI_CreateButton(grp4, 15, 95, 150, 25, "Delete")
	btnapp = GUI_CreateButton(grp4, 15, 95+35, 150, 25, "Apply Texture")
	
	;Transformer
	grp5 = GUI_CreateGroupBox(winmain, 10, 650 , 180, 50 , "Transform")
	rad1 = GUI_CreateRadio(grp5, 10, 20, "Position", 0)
	rad22 = GUI_CreateRadio(grp5, 70, 20, "Scale", 0)
	rad3 = GUI_CreateRadio(grp5, 120, 20, "Rotate", 0)
	GUI_Message(winmain, "SetLocked", True)
End Function


Function Update_ToolBar()
	
	If GUI_AppEvent() = mnu3 Then End
	
	If GUI_AppEvent() = btnnew Then
		mo.Models = CreateModel("models\"+GUI_Message(lbox1, "GetText", 1),"textures\"+GUI_Message(lbox2, "GetText", 1))
	End If
	
	If Gui_AppEvent() = btnnew2 Then
		o.Objects = CreateObject("models\"+GUI_Message(lbox1, "GetText", 1),"textures\"+GUI_Message(lbox2, "GetText", 1))
	End If
	
	If GUI_AppEvent() = btndel Then
		If EntityName(lastse) = "object" Then
			For o.Objects = Each Objects
				If o\model = lastse Then
					DeleteShadowMesh(o\model)
					FreeEntity o\model
					Delete o
					lastse = dummy
					Exit
				End If
			Next
		End If
		
		If EntityName(lastse) = "model" Then
			For mo.Models = Each Models
				If mo\model = lastse Then
					DeleteShadowMesh(mo\model)
					FreeEntity mo\model
					Delete mo
					lastse = dummy
					Exit
				End If
			Next
		End If
	End If
		If GUI_AppEvent() = btnapp Then
		If EntityName(lastse) = "object" Then
			For o.Objects = Each Objects
				If o\model = lastse Then
					If GUI_Message(lbox2, "GetText", 1) <> "NONE" Then
						o\texturepfad = "textures\"+GUI_Message(lbox2, "GetText", 1)
						o\texture = LoadBrush(o\texturepfad)
						PaintEntity o\model,o\texture
						
					Else
						PaintEntity o\model,dummybrush
				EndIf
				End If
			Next
		End If
		
		If EntityName(lastse) = "model" Then
			For mo.Models = Each Models
				If mo\model = lastse Then
					If GUI_Message(lbox2, "GetText", 1) <> "NONE" Then
						FreeTexture mo\texture
						mo\texturepfad = "textures\"+GUI_Message(lbox2, "GetText", 1)
						mo\texture = LoadBrush(mo\texturepfad)
						PaintEntity mo\model,mo\texture
					Else
						PaintEntity mo\model,dummybrush
					EndIf
				End If
			Next
		End If
	End If
	
	
End Function
;Model Funtions
Function CreateModel.Models(modelpfad$,texturepfad$)
	Local mo.Models = New Models
	mo\model = LoadMesh(modelpfad$)
	If texturepfad$ <> "textures\NONE" Then
		mo\texture = LoadBrush(texturepfad$)
		BrushFX mo\texture,2
		PaintEntity mo\model,mo\texture
	EndIf
	mo\modelpfad$ = modelpfad$
	mo\texturepfad$ = texturepfad$
	SetShadowMesh(mo\model)
	EntityPickMode mo\model,2
	NameEntity(mo\model,"model")
	Return mo
End Function

Function CreateObject.Objects(modelpfad$,texturepfad$)
	Local mo.Objects = New Objects
	mo\model = LoadMesh(modelpfad$)
	If texturepfad$ <> "textures\NONE" Then
		mo\texture = LoadBrush(texturepfad$)
		PaintEntity mo\model,mo\texture
	EndIf
	mo\modelpfad$ = modelpfad$
	mo\texturepfad$ = texturepfad$
	SetShadowMesh(mo\model)
	EntityPickMode mo\model,2
	NameEntity(mo\model,"object")
	Return mo
End Function

Function SelectEntity()
	If MouseDown(1) And MouseX() < (1024-200) Then
		If CameraPick(cam,MouseX(),MouseY()) Then
			pe = PickedEntity()
			Return pe
		Else
			Return 0
		End If
	End If
End Function


Function TransSelEntity()
	move# = KeyDown(72)-KeyDown(76)
	strafe# = KeyDown(77)-KeyDown(75)
	high# = KeyDown(78)-KeyDown(74)
	
	If GUI_Message(rad1,"getchecked",1) = 1 Then
		If milli < MilliSecs() Then
			PositionEntity lastse,EntityX(lastse)+strafe/5,EntityY(lastse)+high/5,EntityZ(lastse)+move/5
			milli = MilliSecs()+75
		EndIf
	EndIf
	
	If GUI_Message(rad22,"getchecked",1) = 1 Then
		If milli < MilliSecs() Then
			ResizeMesh(lastse,MeshWidth(lastse)+strafe/3,MeshHeight(lastse)+high/3,MeshDepth(lastse)+move/3)
			milli = MilliSecs()+75
		EndIf
	End If
	
	If GUI_Message(rad3,"getchecked",1) = 1 Then
		If milli < MilliSecs() Then
			RotateEntity lastse,EntityPitch(lastse)+move*(45/2),EntityYaw(lastse)+strafe*(45/2),EntityRoll(lastse)+high*(45/2)
			milli = MilliSecs()+75
		EndIf
	End If
	
End Function


Function ResizeMesh(mesh,width#,height#,depth#)
	
	ScaleMesh mesh,1/MeshWidth#(mesh)*width#,1/MeshHeight#(mesh)*height#,(1/MeshDepth#(mesh))*depth#
	
End Function

;Other Functions
Function Curve#(newvalue#,oldvalue#,increments#)
	Local sign,slip#
	sign=Sgn(oldvalue-newvalue)
	slip#=(oldvalue-newvalue)/increments
	If Abs(slip)<ml slip=ml*Sgn(slip)
		oldvalue=oldvalue-slip
		If sign<>Sgn(oldvalue-newvalue) Return newvalue
			Return oldvalue
End Function


Function SaveMap(pfad$)
	stream = WriteFile(pfad$)	WriteLine stream,"[MODELS]"
	For m.Models = Each Models
		WriteString stream,m\modelpfad$
		WriteString stream,m\texturepfad$
		WriteFloat stream,EntityX(mo\model)
		WriteFloat stream,EntityY(mo\model)
		WriteFloat stream,EntityZ(mo\model)
		WriteFloat stream,MeshWidth(mo\model)
		WriteFloat stream,MeshHeight(mo\model)
		WriteFloat stream,MeshDepth(mo\model)
		WriteFloat stream,EntityPitch(mo\model)
		WriteFloat stream,EntityYaw(mo\model)
		WriteFloat stream,EntityRoll(mo\model)
		WriteLine stream,""
	Next
	WriteLine(stream,"[OBJECTS]")
	For o.Objects = Each Objects
		WriteString stream,o\modelpfad
		WriteString stream,o\texturepfad
		WriteFloat stream,EntityX(o\model)
		WriteFloat stream,EntityY(o\model)
		WriteFloat stream,EntityZ(o\model)
		WriteFloat stream,MeshWidth(o\model)
		WriteFloat stream,MeshHeight(o\model)
		WriteFloat stream,MeshDepth(o\model)
		WriteFloat stream,EntityPitch(o\model)
		WriteFloat stream,EntityYaw(o\model)
		WriteFloat stream,EntityRoll(o\model)
		WriteLine stream,""
	Next
End Function


;!TYPES
Type Models
	Field x#,y#,z#
	Field sx#,sy#,sz#
	Field model,modelpfad$,texture,texturepfad$
End Type

Type Objects
	Field x#,y#,z#
	Field sx#,sy#,sz#
	Field rotx#,roty#,rotz#
	Field model,modelpfad$,texture,texturepfad$
End Type
;~IDEal Editor Parameters:
;~C#Blitz3D